package pers.cz.selenium.component;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import pers.cz.websocket.MessagePushSocketUtils;

/**
 * @program: PostGirl-panent
 * @description: 弹框控件
 * @author: Cheng Zhi
 * @create: 2022-12-27 17:37
 **/
public class AlertBoxComponent extends AbstractComponent {

    private RemoteWebDriver driver;
    private WebElement webElement;

    public AlertBoxComponent(String fullXPath, RemoteWebDriver driver) {
        this.driver = driver;
        this.webElement = driver.findElementByXPath(fullXPath);
        init();
    }

    @Override
    public void click() {
        driver.switchTo().alert().accept();
    }

    /**
     * 获取弹出内容
     * @return
     */
    public String getAlertValue() {
        String text = driver.switchTo().alert().getText();
        MessagePushSocketUtils.sendMsg( MessagePushSocketUtils.AUTOUI_LOG_MESSAGE, text, 0);
        return text;
    }

    public void closeAlert() {
        webElement.click();
    }
    @Override
    public WebElement initWebElement() {
        return webElement;
    }

    @Override
    public RemoteWebDriver initDriver() {
        return driver;
    }
}
